# 各城市岗位数量图
import pymysql
from pyecharts import options
from pyecharts.charts import Pie

db = pymysql.connect(
    host="localhost",
    port=3306,
    user="root",
    # database="crawlerdemo",
    password="123456",
    charset="utf8"
)
cursor = db.cursor()
# 用来查找岗位数量最多的五个城市
sql = "select job_place,count(job_id) from crawlerdemo.job group by job_place order by count(job_id) desc limit 5"
num=0
try:
    cursor.execute(sql)
    res = cursor.fetchall()
    for i in res:
        num=num+i[1]
        print(num)
    sql1 = ("select job_place,count(job_id)/%d*100 from crawlerdemo.job group by job_place order by count(job_id) desc limit 5") %num
    print(sql1)
    cursor.execute(sql1)
    res1 = cursor.fetchall()
    pie = Pie()
    # [("类型一",30),("类型2",40),(),()....]
    pie.add("单位/百分比",res1)
    pie.set_global_opts(title_opts=options.TitleOpts(title="各个城市的岗位占比"))
    pie.render("piera.html")
except:
    print("查询出问题了")